<template>
  <div class="find-goods">
    <div class="section-header">
      <h3>
        <i class="fas fa-search"></i>
        <span>找货专区</span>
      </h3>
      <NuxtLink to="/find-goods" class="more-link">
        <span>发布找货信息</span>
        <i class="fas fa-angle-double-right"></i>
      </NuxtLink>
    </div>
    
    <div class="goods-list" @mouseenter="stopCarousel" @mouseleave="startCarousel">
        <div 
          v-for="item in findGoodsList" 
          :key="item.id"
          class="goods-item"
          @click="getPhone(item)"
        >
        <div class="goods-header">
          <span class="category-tag">{{ item.category }}</span>
          <span class="urgent-tag" v-if="item.urgent">急需</span>
        </div>
        <h4 class="goods-title">{{ item.title }}</h4>
        <div class="goods-info">
          <div class="info-item">
            <i class="fas fa-box"></i>
            <span>需求量：{{ item.quantity }}</span>
          </div>
          <div class="info-item">
            <i class="fas fa-dollar-sign"></i>
            <span>预算：{{ item.budget }}</span>
          </div>
          <div class="info-item">
            <i class="fas fa-map-marker-alt"></i>
            <span>{{ item.location }}</span>
          </div>
        </div>
        <div class="goods-footer">
          <div class="publisher">
            <img :src="item.avatar" :alt="item.publisher" />
            <span>{{ item.publisher }}</span>
          </div>
          <span class="publish-time">{{ item.publishTime }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
const allGoodsList = [
  {
    id: 1,
    category: '香菇',
    title: '长期收购优质干香菇，要求肉厚无杂质',
    quantity: '2000斤/月',
    budget: '面议',
    location: '湖北武汉',
    publisher: '张先生',
    avatar: '/images/avatar-1.jpg',
    publishTime: '2小时前',
    urgent: true,
  },
  {
    id: 2,
    category: '木耳',
    title: '求购东北黑木耳，质量好价格优',
    quantity: '500斤',
    budget: '¥80-100/斤',
    location: '广东深圳',
    publisher: '李女士',
    avatar: '/images/avatar-2.jpg',
    publishTime: '5小时前',
    urgent: false,
  },
  {
    id: 3,
    category: '菌种',
    title: '需要平菇菌种，量大从优',
    quantity: '1000袋',
    budget: '¥5-8/袋',
    location: '河南郑州',
    publisher: '王老板',
    avatar: '/images/avatar-3.jpg',
    publishTime: '1天前',
    urgent: true,
  },
  {
    id: 4,
    category: '金针菇',
    title: '寻找金针菇供应商，需长期合作',
    quantity: '3000斤/周',
    budget: '¥12-15/斤',
    location: '上海',
    publisher: '陈经理',
    avatar: '/images/avatar-4.jpg',
    publishTime: '1天前',
    urgent: false,
  },
  {
    id: 5,
    category: '平菇',
    title: '收购新鲜平菇，要求品质优良',
    quantity: '1500斤',
    budget: '¥8-12/斤',
    location: '山东济南',
    publisher: '刘总',
    avatar: '/images/avatar-1.jpg',
    publishTime: '2天前',
    urgent: false,
  },
  {
    id: 6,
    category: '杏鲍菇',
    title: '大量收购杏鲍菇，工厂直供',
    quantity: '5000斤/月',
    budget: '面议',
    location: '江苏南京',
    publisher: '王经理',
    avatar: '/images/avatar-2.jpg',
    publishTime: '3天前',
    urgent: true,
  },
  {
    id: 7,
    category: '茶树菇',
    title: '求购茶树菇干货，出口品质',
    quantity: '2000斤',
    budget: '¥25-35/斤',
    location: '福建福州',
    publisher: '李总',
    avatar: '/images/avatar-3.jpg',
    publishTime: '4天前',
    urgent: false,
  },
  {
    id: 8,
    category: '猴头菇',
    title: '收购猴头菇，药用级品质',
    quantity: '800斤',
    budget: '¥45-60/斤',
    location: '云南昆明',
    publisher: '赵经理',
    avatar: '/images/avatar-4.jpg',
    publishTime: '5天前',
    urgent: false,
  },
  {
    id: 9,
    category: '银耳',
    title: '求购古田银耳，出口品质要求',
    quantity: '1000斤',
    budget: '¥30-45/斤',
    location: '浙江杭州',
    publisher: '周总',
    avatar: '/images/avatar-1.jpg',
    publishTime: '6天前',
    urgent: false,
  },
  {
    id: 10,
    category: '竹荪',
    title: '收购竹荪干货，药用级标准',
    quantity: '500斤',
    budget: '¥60-80/斤',
    location: '四川成都',
    publisher: '吴经理',
    avatar: '/images/avatar-2.jpg',
    publishTime: '7天前',
    urgent: true,
  },
  {
    id: 11,
    category: '松茸',
    title: '求购野生松茸，高端市场',
    quantity: '200斤',
    budget: '¥200-300/斤',
    location: '云南昆明',
    publisher: '郑总',
    avatar: '/images/avatar-3.jpg',
    publishTime: '8天前',
    urgent: false,
  },
  {
    id: 12,
    category: '牛肝菌',
    title: '收购牛肝菌，火锅食材专用',
    quantity: '800斤',
    budget: '¥40-60/斤',
    location: '贵州贵阳',
    publisher: '何经理',
    avatar: '/images/avatar-4.jpg',
    publishTime: '9天前',
    urgent: false,
  }
]

// 显示的商品列表（轮播用）
const findGoodsList = ref([])
let carouselTimer = null

// 初始化显示前6个商品
const initGoodsList = () => {
  findGoodsList.value = allGoodsList.slice(0, 6)
}

// 轮播功能
const startCarousel = () => {
  carouselTimer = setInterval(() => {
    // 移除第一个商品
    findGoodsList.value.shift()
    
    // 找到下一个要添加的商品
    const currentLastId = findGoodsList.value[findGoodsList.value.length - 1]?.id || 0
    const nextGoodsIndex = allGoodsList.findIndex(goods => goods.id === currentLastId) + 1
    const nextGoods = allGoodsList[nextGoodsIndex % allGoodsList.length]
    
    // 添加新的商品到底部
    findGoodsList.value.push(nextGoods)
  }, 4000) // 每4秒轮播一次
}

const stopCarousel = () => {
  if (carouselTimer) {
    clearInterval(carouselTimer)
    carouselTimer = null
  }
}

// 组件挂载时开始轮播
onMounted(() => {
  initGoodsList()
  startCarousel()
})

// 组件卸载时清除定时器
onUnmounted(() => {
  stopCarousel()
})

const getPhone = (item) => {
  // 模拟获取电话号码的过程
  const phoneNumber = item.publisher === '张先生' ? '138****1234' :
                     item.publisher === '李女士' ? '139****5678' :
                     item.publisher === '王老板' ? '137****9012' :
                     item.publisher === '陈经理' ? '136****3456' :
                     item.publisher === '刘总' ? '135****7890' :
                     item.publisher === '王经理' ? '134****2345' :
                     item.publisher === '李总' ? '133****6789' :
                     item.publisher === '赵经理' ? '132****0123' :
                     item.publisher === '周总' ? '131****4567' :
                     item.publisher === '吴经理' ? '130****8901' :
                     item.publisher === '郑总' ? '129****2345' :
                     item.publisher === '何经理' ? '128****6789' : '138****0000'
  
  // 复制电话号码到剪贴板
  navigator.clipboard.writeText(phoneNumber).then(() => {
    // 显示成功提示
    showToast(`联系电话已复制：${phoneNumber}`)
  }).catch(() => {
    // 如果复制失败，显示电话号码
    showToast(`联系电话：${phoneNumber}`)
  })
}

// 显示提示信息
const showToast = (message) => {
  // 创建提示元素
  const toast = document.createElement('div')
  toast.textContent = message
  toast.style.cssText = `
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 12px 20px;
    border-radius: 6px;
    font-size: 14px;
    z-index: 10000;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  `
  
  document.body.appendChild(toast)
  
  // 2秒后移除提示
  setTimeout(() => {
    document.body.removeChild(toast)
  }, 2000)
}

const viewDetail = (id) => {
  navigateTo(`/find-goods/${id}`)
}
</script>

<style lang="scss" scoped>
.find-goods {
  .section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
    
    h3 {
      display: flex;
      align-items: center;
      gap: 8px;
      margin: 0;
      font-size: 20px;
      font-weight: 600;
      color: #333;
      
      i {
        color: #e4393c;
        font-size: 22px;
      }
    }
    
    .more-link {
      display: flex;
      align-items: center;
      gap: 6px;
      color: #666;
      font-size: 14px;
      text-decoration: none;
      transition: all 0.3s;
      
      &:hover {
        color: #e4393c;
      }
    }
  }
  
  .goods-list {
    display: flex;
    gap: 20px;
    overflow: hidden;
    position: relative;
    height: 240px; /* 固定高度防止布局跳动 */

    @include tablet {
      gap: 18px;
      
      .goods-item {
        width: 240px;
        min-width: 240px;
      }
    }

    @include mobile {
      gap: 15px;
      
      .goods-item {
        width: 200px;
        min-width: 200px;
      }
    }
    
    .goods-item {
      background: #fff;
      border-radius: 8px;
      padding: 15px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
      cursor: pointer;
      transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
      border: 1px solid transparent;
      flex-shrink: 0;
      width: 220px;
      min-width: 220px;
      
      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
        border-color: #1890ff;
      }
      
        .goods-header {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 12px;
        
        .category-tag {
          padding: 3px 8px;
          background: #f0f0f0;
          color: #666;
          font-size: 12px;
          border-radius: 3px;
        }
        
        .urgent-tag {
          padding: 3px 8px;
          background: #e4393c;
          color: #fff;
          font-size: 12px;
          border-radius: 3px;
        }
      }
      
      .goods-title {
        margin: 0 0 12px;
        font-size: 14px;
        font-weight: 600;
        color: #333;
        line-height: 1.4;
        height: 40px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      
      .goods-info {
        margin-bottom: 15px;
        
        .info-item {
          display: flex;
          align-items: center;
          gap: 6px;
          font-size: 12px;
          color: #666;
          margin-bottom: 6px;
          
          &:last-child {
            margin-bottom: 0;
          }
          
          i {
            color: #e4393c;
            font-size: 12px;
            width: 14px;
          }
        }
      }
      
      .goods-footer {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-top: 12px;
        border-top: 1px solid #f0f0f0;
        
        .publisher {
          display: flex;
          align-items: center;
          gap: 8px;
          font-size: 12px;
          color: #666;
          
          img {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            object-fit: cover;
          }
        }
        
        .publish-time {
          font-size: 11px;
          color: #999;
        }
      }
    }
  }
}
</style>
